package com.walle.concurrenttest.pattern.singlethreadexecute;

/**
 * @author zhangjiapeng
 * @Package com.walle.concurrenttest.pattern.singlethreadexecute
 * @Description: ${todo}
 * @date 2019/2/18 13:53
 */
public class EaterThread implements Runnable {

    private final String name;
    private final Tool right;
    private final Tool left;


    public EaterThread(String name, Tool right, Tool left) {
        this.name = name;
        this.right = right;
        this.left = left;
    }

    @Override
    public void run() {

        while (true){
            synchronized (left){
                System.out.println(name + "左手拿到了" +left.getName());
                synchronized (right){
                    System.out.println(name + "右手拿到了" +right.getName());
                    System.out.println(name + "开始吃东西");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(name  + "吃完了，放下了右手的"+right.getName());
                }
                System.out.println(name  + "吃完了，放下了左手的"+left.getName());
            }
        }
    }
}
